package com.xxyy.mylive.http;

import android.os.Handler;
import android.os.Message;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 网络请求工具类
 * Created by ado on 2:37 2016/10/26.
 */
public class OkHttpUtils {
    public static void httpGet(String url, final Handler handler, final int acion) {
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Message msg = Message.obtain();
                    msg.what = acion;
                    msg.obj = response.body().string();
                    handler.sendMessage(msg);
                }
            }
        });
    }
}
